package org.example.myportfolio.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.example.myportfolio.config.response.JsonResult;
import org.example.myportfolio.dao.mapper.UserMapper;
import org.example.myportfolio.dao.repository.UserRepository;
import org.example.myportfolio.pojo.param.LoginParam;
import org.example.myportfolio.pojo.po.UserPO;
import org.example.myportfolio.pojo.vo.UserVO;
import org.example.myportfolio.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRepository userRepository;

    @Override
    public JsonResult login(LoginParam loginParam) {
        log.debug("用户登录入参：{}",loginParam.getName());
        //判断用户名和密码
        String username = loginParam.getName();
        UserPO userPO = userRepository.selectUserByUsername(username);
        UserVO userVO = new UserVO();
        if(ObjectUtils.isEmpty(userPO)){
            return JsonResult.fail("用户名或密码错误！");
        }else if(userPO.getPassword().equals(loginParam.getPassword())){
            log.debug("登录成功");
            BeanUtils.copyProperties(userPO,userVO);
            return JsonResult.ok(userVO);
        }
        return JsonResult.fail("用户名或密码错误！");

    }
}
